package com.ccc.service;

import com.ccc.bean.Customer;

import java.util.Arrays;

public class CustomerService {
    private Customer[] customerList = new Customer[5];

    private static final CustomerService CUSTOMER_SERVICE = new CustomerService();


    private CustomerService() {

    }

    public static CustomerService getCustomerService() {
        return CUSTOMER_SERVICE;
    }

    private int count = 0;

    public boolean saveCustomer(Customer customer) {

        if (count >= customerList.length) { // 容量不够大 扩容
            customerList = Arrays.copyOf(customerList, customerList.length * 2);
        }
        customerList[count++] = customer;
        return true;
    }

    public Customer[] getAllCustomer() {
        return Arrays.copyOf(customerList, count);
    }

    public int getCustomerById(int id) {
        for (int i = 0; i < customerList.length; i++)
            if (customerList[i].getId() == id) return i;

        return -1;
    }

    public boolean deleteCustomerByIndex(int index) {
        if (index < 0 || index >= count) return false;
        for (int i = index; i < count - 1; i++) {
            customerList[i] = customerList[i + 1];
        }
        customerList[count - 1] = null;
        count--;
        return true;
    }
}
